package com.dmzc.sort;

import java.util.HashMap;

//插入排序
public class InsertSort {
    public static void main(String[] args) {
        Node1[] node1s = new Node1[5];
        Node1 node1 = new Node1(5, 6);
        node1s[0] = node1;
        node1 = new Node1(5, 5);
        node1s[1] = node1;
        node1 = new Node1(1, 3);
        node1s[2] = node1;
        node1 = new Node1(3, 2);
        node1s[3] = node1;
        node1 = new Node1(2, 1);
        node1s[4] = node1;
        int i = insertSort(node1s);
        System.out.println(i);
        for (int j = 0; j < node1s.length; j++) {
            System.out.println(node1s[j]);
        }
    }

    public static int insertSort(Node1[] node1s) {
        int count = 0;
        if (node1s.length <= 1) return count;
        for (int i = 1; i < node1s.length; i++) {
            Node1 node1 = node1s[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (node1s[j].index > node1.index) {
                    node1s[j + 1] = node1s[j];
                    count++;
                } else {
                    break;
                }
            }
            node1s[j + 1] = node1;
        }
        return count;
    }
}
